package com.lfg.leetcode.leetcode200;

public class No238productExceptSelf {

    public static void main(String[] args) {
        int[] ret = productExceptSelf(new int[]{1, 2, 3, 0});
        for (int i : ret) {
            System.out.println(i);
        }
    }

    public static int[] productExceptSelf(int[] nums) {
        int[] ret = new int[nums.length];

        if (nums.length == 0) {
            return ret;
        }

        int[] left = new int[nums.length];
        int[] right = new int[nums.length];
        int total = 1;
        for (int i = 0; i < nums.length; i++) {
            total *= nums[i];
            left[i] = total;
        }
        total = 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            total *= nums[i];
            right[i] = total;
        }

        ret[0] = right[1];
        ret[nums.length - 1] = left[nums.length - 2];

        for (int i = 1; i < nums.length - 1; i++) {
            ret[i] = left[i - 1] * right[i + 1];
        }
        return ret;
    }

}
